//
//  ViewController.m
//  testSyntextApp
//
//  Created by MICHAEL on 15/7/21.
//  Copyright (c) 2015年 kyy. All rights reserved.
//

#import "ViewController.h"
#import "SelectColorViewController.h"
#import "TestAutoLayoutViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *vcMutableArray;
@property UITableView *mainTableView;
@end

@implementation ViewController
-(NSMutableArray *)vcMutableArray{
    if (!_vcMutableArray) {
        _vcMutableArray=[[NSMutableArray alloc]init];
    }
    return _vcMutableArray;

}
-(void)testFatherController{

    NSLog(@"father controller");
}

- (void)viewDidLoad {
 [super viewDidLoad];
    NSBundle *bundle=[NSBundle mainBundle];
    NSString *plistPth=[bundle pathForResource:@"team" ofType:@"plist"];
    NSDictionary *teamDictionary=[NSDictionary dictionaryWithContentsOfFile:plistPth];
    NSLog(@"teamArray is%@",teamDictionary);
    NSArray *array=[teamDictionary allKeys];
    NSLog(@"array==%@",array);
    array= [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"array after==%@",array);

    NSArray *vcNameArray=@[@"SelectColorViewController",@"TestAutoLayoutViewController"];
    for (int i=0; i<vcNameArray.count; i++) {
        id viewController;
        NSLog(@"%@",[NSClassFromString(vcNameArray[i]) class]);
         viewController =[[NSClassFromString(vcNameArray[i]) alloc]init];
        if ([viewController isKindOfClass:[UIViewController class]]==YES) {

            [self.vcMutableArray addObject:viewController];
        }else{
            NSLog(@"not in array%@",[NSClassFromString(vcNameArray[i]) class]);
            NSLog(@"%@",[UIViewController class]);
        }
    }
    self.mainTableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.mainTableView.delegate=self;
    self.mainTableView.dataSource=self;
    [self.view addSubview:self.mainTableView];




    // Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -table view 代理方法
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"questionCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text=[(self.vcMutableArray[indexPath.row]) description];


//    switch (indexPath.row) {
//        case 0:
//        {
//            cell.textLabel.text=@"请选择你喜欢的颜色";
//
//            self.success=^(NSString*string){
//
//            self.view.backgroundColor=[UIColor greenColor];
//                cell.textLabel.text=string;
//                
//                
//            };
//
//
//        }
//        case 1:{
//            cell.textLabel.text=@"测试autolayout";
//
//        }
//            break;
//
//        default:
//            break;
//    }

        return cell;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.vcMutableArray.count;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    id viewController;
    viewController=[self.vcMutableArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:viewController animated:YES];
//    switch (indexPath.row) {
//        case 0:
//        {
//            SelectColorViewController *select=[[SelectColorViewController alloc]init];
//            [self.navigationController pushViewController:select animated:YES];
//
//        }
//            break;
//        case 1:{
//            TestAutoLayoutViewController *testAutoLayoutVC=[[TestAutoLayoutViewController alloc]init];
//            [self.navigationController pushViewController:testAutoLayoutVC animated:YES];
//
//
//            
//        }
//            break;
//        default:
//            break;
//    }

//    dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    size_t numberOfIteration=100;
//    dispatch_async(queue, ^(void) {
//
//        dispatch_apply(numberOfIteration, queue, ^(size_t iteration){
//            [self doCalculate];
//        });
//
//    });

    //[NSThread detachNewThreadSelector:@selector(calculate) toTarget:self withObject:nil];


}
-(void)calculate{
    @autoreleasepool {
        NSInteger couter=0;
        while ([[NSThread currentThread]isCancelled]==NO) {
            [self doCalculate];
            couter++;
            if (couter>=100) {
                break;
            }
        }
    }

}
-(void)doCalculate{

    NSLog(@"do calculate");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
